<?php

class Core_Model_Customer_Shop extends Dcmp_Model_Status {

    public function __construct() {
        parent::__construct();
    }

    protected function getTableClass() {
        return Kd_Loader::loadClass('table_customer_shop');
    }

    public function loadByPoi($poi) {
        $this->fill($this->getTable()->getByPoi($poi));
        return $this;
    }

    public function getPoiInfo() {
        if (!$this->hasData('poi_info')) {
            $this->data['poi_info'] = App::getModel('foundation/poi')->load($this->getPoiId());
        }
        return $this->data['poi_info'];
    }

    public function afterInsert($transaction) {
        if ($this->getKeyword()) {
            App::getHelper('tag')->registerSearchableRecord(explode(' ', $this->getTag()), $this->getPoiId(), 'customer');
        }
        if ($this->issetData('sef_url')) {
            App::getHelper('website')->addSefUrl('card', $this->getPoiId(), '/customer/shop/card/id/' . $this->getPoiId(), '/card/' . $this->getSefUrl());
        }
    }

    public function afterUpdate($transaction) {
        if ($this->propertyIsChanged('tag')) {
            App::getHelper('tag')->unregisterSearchableRecord($this->getPoiId(), 'customer')
                                 ->registerSearchableRecord(explode(' ', $this->getTag()), $this->getPoiId(), 'customer');
        }
		if ($this->propertyIsChanged('sef_url')) {
            App::getHelper('website')->deleteSefUrl('card', $this->getId());
			App::getHelper('website')->addSefUrl('card', $this->getPoiId(), '/customer/shop/card/id/' . $this->getPoiId(), '/card/' . $this->getSefUrl());
        }   
    }

    protected function initializeTable($table) {
        $table->setName('{t}customer_shop');
        $table->createColumn('id', 'string', TRUE, TRUE);
        $table->createColumn('introduction', 'string');
        $table->createColumn('detail', 'string');
        $table->createColumn('signmap_name', 'string');
        $table->createColumn('signmap_picture', 'string');
        $table->createColumn('poi_id', 'string');
        $table->createColumn('tag', 'string');
        $table->createColumn('ebook_url', 'string');
        $table->createColumn('sef_url', 'string');
        $table->createColumn('location_id', 'string');
        parent::initializeTable($table);
    }

}